unit Unit1;

interface

{$IFNDEF SAX_WIDESTRINGS}
This demo requires that you activate SAX_WIDESTRINGS within the Project | Options
Conditional Defines section (after you specify the flag, build all)
{$ENDIF}

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, SAXComps, SAX, BSAX;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    SAXErrorHandler1: TSAXErrorHandler;
    SAXContentHandler1: TSAXContentHandler;
    procedure SAXErrorHandler1Error(Sender: TObject;
      const Error: ISAXParseError);
    procedure SAXErrorHandler1FatalError(Sender: TObject;
      const Error: ISAXParseError);
    procedure SAXErrorHandler1Warning(Sender: TObject;
      const Error: ISAXParseError);
    procedure SAXContentHandler1StartElement(Sender: TObject;
      const NamespaceURI, LocalName, QName: WideString;
      const Atts: IAttributes);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses SAXAdapters;

procedure TForm1.SAXErrorHandler1Error(Sender: TObject;
  const Error: ISAXParseError);
begin
  ListBox1.Items.Add('[Error] ' + Error.getMessage + ' Line ' +
    IntToStr(Error.getLineNumber) + ' Column ' + IntToStr(Error.getColumnNumber));
end;

procedure TForm1.SAXErrorHandler1FatalError(Sender: TObject;
  const Error: ISAXParseError);
begin
  ListBox1.Items.Add('[Fatal Error] ' + Error.getMessage + ' Line ' +
    IntToStr(Error.getLineNumber) + ' Column ' + IntToStr(Error.getColumnNumber));
end;

procedure TForm1.SAXErrorHandler1Warning(Sender: TObject;
  const Error: ISAXParseError);
begin
  ListBox1.Items.Add('[Warning] ' + Error.getMessage + ' Line ' +
    IntToStr(Error.getLineNumber) + ' Column ' + IntToStr(Error.getColumnNumber));
end;

procedure TForm1.SAXContentHandler1StartElement(Sender: TObject;
  const NamespaceURI, LocalName, QName: WideString;
  const Atts: IAttributes);
begin
  ListBox1.Items.Add('[Element] ' + QName);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  XMLReader : IXMLReader;
  XMLBufReader: IBufferedXMLReader;
  Vendor: TSAXVendor;
  ContentHandler: IContentHandler;
begin
  ListBox1.Items.Clear;
  ContentHandler:= SAXContentHandler1;
  if (OpenDialog1.Execute) then
  begin
    // Get the Default SAX Vendor
    Vendor:= GetSAXVendor;
    if Vendor is TBufferedSAXVendor then
    begin
      XMLBufReader:= TBufferedSAXVendor(Vendor).BufferedXMLReader;
      XMLBufReader.setContentHandler(Adapt(ContentHandler, XMLBufReader));
      XMLBufReader.setErrorHandler(SAXErrorHandler1);
      XMLBufReader.parse(OpenDialog1.FileName);
      XMLBufReader:= nil;
    end else
    begin
      XMLReader:= Vendor.XMLReader;
      XMLReader.setContentHandler(ContentHandler);
      XMLReader.setErrorHandler(SAXErrorHandler1);
      XMLReader.parse(OpenDialog1.FileName);
      XMLReader:= nil;
    end;
  end;
end;

end.
